<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Blog extends Controller_Template {

	public $template = 'blog/template';
	private $page;
	private $uri;
	private $index;

	public function before()
	{
		parent::before();
		
		// Set the language
		I18n::$lang = Cookie::get('lang', 'ru-ru');

		$this->template->uri = $this->request->controller.'/'.$this->request->action;
		$this->page = Page::findByLink($this->template->uri);
		$this->index = Route::get('blog');

		$this->template->title = $this->page->title;
		$this->template->keywords = $this->page->keywords;
		$this->template->description = $this->page->description;
	}

	public function action_index()
	{
		$this->template->content = View::factory('blog/content/index')
			->set('blogs', Blog::findAll());

		$profile = ORM::factory('user', 1)->with('user_profiles');

		// Bind the breadcrumb
		$this->template->bind('breadcrumb', $breadcrumb);
		$breadcrumb = array();
	}

	public function action_view($slug)
	{
		$blog = Blog::findBySlug($slug);
		$this->page = Page::findByParent($this->template->uri);
		
		$this->template->title = $blog->title;
		$this->template->keywords = $blog->keywords;
		$this->template->description = $blog->description;
		
		$this->template->content = View::factory('blog/content/view')
			->set('blog', $blog);

		// Bind the breadcrumb
		$this->template->bind('breadcrumb', $breadcrumb);

		$breadcrumb = array();
		$breadcrumb[$this->index->uri(array('page' => NULL))] = $this->page->title;
		$breadcrumb[] = $this->template->title;
	}

	public function action_rss()
	{
		$this->request->send_headers('text/xml');
		$this->template = View::factory('blog/rss');

		$this->template->title = $this->page->title;
		$this->template->keywords = $this->page->keywords;
		$this->template->description = $this->page->description;
		$this->template->blogs = Blog::findAll();
	}

	public function after()
	{
		$this->template->styles = array(
			'public/css/style.css'  => 'screen',
		);

		// Add scripts
		$this->template->scripts = array(
			"public/js/jquery.min.js",
			"public/js/jquery.cookie.js",
			"public/js/main.js",
		);

		debug::all();
		parent::after();
	}

} // End Welcome